package com.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.pojo.User;
import com.pojo.Uservehicle;
import com.pojo.vo.vo_ResponseBody;
import com.service.LoginService;
import com.service.RegService;
import com.utils.StringUtil;
import com.utils.UserAuth;

import rest.RestTest;

@RequestMapping("userController")
@Controller
public class userController {
	
	@Autowired
	private LoginService loginService;
	
	@Autowired
	private RegService regService;
	
	public userController(){
		System.out.println("loginController 初始化");
	}
	
	@RequestMapping(value="login",produces="application/json;charset=UTF-8",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String login(String userAuth) {
		String data="";
		try{
			UserAuth realUserAuth=JSONArray.parseObject(userAuth,UserAuth.class);
			System.out.println("=====");
			System.out.println(realUserAuth.getUserType_id()+" "+realUserAuth.getUserName()+" "+realUserAuth.getUserPass());
			System.out.println("=====");
			data=loginService.login(realUserAuth);
		}catch(Exception e){
			e.printStackTrace();
			return JSONArray.toJSONString(new vo_ResponseBody(0,-1,"服务器开小差啦"));
		}

		return data;
	}
	
	@RequestMapping(value="reg",produces="application/json;charset=UTF-8",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String reg(String phoneImei,String code,String user,String uservehicle,String userType_id) {
		System.out.println(user);
		System.out.println(userType_id);
		System.out.println(uservehicle);
		String data=JSONArray.toJSONString(new vo_ResponseBody(0,-1,"注册失败"));
		try{
			Uservehicle realUservehicle=JSONArray.parseObject(uservehicle, Uservehicle.class);
			User realUser=JSONArray.parseObject(user, User.class);
			data=regService.reg(phoneImei,code,realUser,userType_id,realUservehicle);
		}catch(Exception e){
			data=JSONArray.toJSONString(new vo_ResponseBody(0,-1,"注册失败"));
		}
		return data;
	}
	
	@RequestMapping(value="reg_getCheckcode",produces="text/html;charset=UTF-8",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String reg_getCheckcode(String phoneImei,String mobile) {
		String data="获取验证码失败";
		try{
			data=loginService.reg_getCheckcode(phoneImei,mobile);
		}catch(Exception e){
			return JSONArray.toJSONString(new vo_ResponseBody(0, -1, "获取验证码异常"));
		}
		return data;
	}
	
	@RequestMapping(value="getUserAndType",produces="text/html;charset=UTF-8",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String getUserAndType(String user_CardId_hash){
		String data="";
		System.out.println(user_CardId_hash);
		try{
			data=loginService.getUserAndType(user_CardId_hash);
		}catch (Exception e) {
			// TODO: handle exception
			data=JSONArray.toJSONString(new vo_ResponseBody(0,-1,"服务器异常"));
		}
		return data;
	}
	
	@RequestMapping(value="edit",produces="text/html;charset=UTF-8",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String edit(String user,Integer user_type_id,String uservehicle){
		System.out.println(user);
		System.out.println(user_type_id);
		System.out.println(uservehicle);
		String data=JSONArray.toJSONString(new vo_ResponseBody(0,0,"修改成功"));
		try{
			User editUser=JSONArray.parseObject(user, User.class);
			Uservehicle editUservehicle=JSONArray.parseObject(uservehicle, Uservehicle.class);
			data=loginService.edit(editUser,user_type_id,editUservehicle);
		}catch (Exception e) {
			// TODO: handle exception
			data=JSONArray.toJSONString(new vo_ResponseBody(0,-1,"系统出错啦"));
		}
		return data;
	}
	
}
